package com.ruoyi.common.utils;

import net.coobird.thumbnailator.Thumbnails;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.Base64;

/**
 * Base64 转换工具
 */
public class Base64Utils {
	/**
	 * BASE64解密
	 *
	 * @throws Exception
	 */
	public static String decryptBASE64(String encoded) {
		// Base64 解密
		byte[] decoded = Base64.getDecoder().decode(encoded.replaceAll(" ", ""));

		String decodeStr = new String(decoded);
		System.out.println("Base 64 解密后：" + decodeStr);

		return decodeStr;
	}

	/**
	 * BASE64加密
	 */
	public static String encryptBASE64(String str) {
		byte[] bytes = str.getBytes();

		// Base64 加密
		String encoded = Base64.getEncoder().encodeToString(bytes);
		System.out.println("Base 64 加密后：" + encoded);
		return encoded;
	}

	/**
	 * byte数组 转换为 Base64字符串
	 */
	@SuppressWarnings("restriction")
	public static String encode(byte[] data) {
		return new BASE64Encoder().encode(data);
	}

	/**
	 * Base64字符串 转换为 byte数组
	 */
	@SuppressWarnings("restriction")
	public static byte[] decode(String base64) {
		try {
			return new BASE64Decoder().decodeBuffer(base64);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new byte[0];
	}

	/**
	 * 把文件内容编码为 Base64字符串, 只能编码小文件（例如文本、图片等）
	 */
	public static String encodeFile(File file) throws Exception {
		InputStream in = null;
		ByteArrayOutputStream bytesOut = null;

		try {
			in = new FileInputStream(file);
			bytesOut = new ByteArrayOutputStream((int) file.length());

			byte[] buf = new byte[1024];
			int len = -1;

			while ((len = in.read(buf)) != -1) {
				bytesOut.write(buf, 0, len);
			}
			bytesOut.flush();

			return encode(bytesOut.toByteArray());

		} finally {
			close(in);
			close(bytesOut);
		}
	}

	/**
	 * 把 Base64字符串 转换为 byte数组, 保存到指定文件
	 */
	public static void decodeFile(String base64, File file) throws Exception {
		OutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(file);
			fileOut.write(decode(base64));
			fileOut.flush();
		} finally {
			close(fileOut);
		}
	}

	private static void close(Closeable c) {
		if (c != null) {
			try {
				c.close();
			} catch (IOException e) {
				// nothing
			}
		}
	}

	/**
	 * compressImage
	 * 
	 * @param path
	 * @param ppi
	 * @return
	 */
	@SuppressWarnings("restriction")
	public static String compressImage(String path, int ppi) {
		byte[] smallImage = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			Thumbnails.of(path).size(ppi, ppi).outputFormat("jpg").toOutputStream(out);
			smallImage = out.toByteArray();
		} catch (IOException e) {
			// TODO: handle exception
		}

		return new BASE64Encoder().encode(smallImage);
	}

	public static File compressImg(String path, String outPath, int ppi) {
		try {
			File file = new File(outPath);
			if (file.exists()) {
				file.delete();
			}
			Thumbnails.of(path).size(ppi, ppi).keepAspectRatio(false).allowOverwrite(true).toFile(outPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new File(path);
		}
		return new File(outPath);
	}

}